// XePS2.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "CDVDRaw.h"

D3DDevice*             g_pd3dDevice;    // Our rendering device
D3DVertexBuffer*       g_pVB;           // Buffer to hold vertices
D3DVertexDeclaration*  g_pVertexDecl;   // Vertex format decl
D3DVertexShader*       g_pVertexShader; // Vertex Shader
D3DPixelShader*        g_pPixelShader;  // Pixel Shader
D3DPRESENT_PARAMETERS d3dpp;

XMMATRIX g_matWorld;
XMMATRIX g_matProj;
XMMATRIX g_matView;

BOOL g_bWidescreen = TRUE;

class CMyApp : public CXuiModule {
protected:
	virtual HRESULT RegisterXuiClasses();
	virtual HRESULT UnregisterXuiClasses();
};

HRESULT CMyApp::RegisterXuiClasses() {
	CChangeDirectoryScene::Register();
	return CMyMainScene::Register();
}

HRESULT CMyApp::UnregisterXuiClasses() {
	CChangeDirectoryScene::Unregister();
	CMyMainScene::Unregister();
	return S_OK;
}

HRESULT InitD3D()
{
    // Create the D3D object.
    Direct3D* pD3D = Direct3DCreate9( D3D_SDK_VERSION );
    if( !pD3D )
        return E_FAIL;

    // Set up the structure used to create the D3DDevice.
    ZeroMemory( &d3dpp, sizeof(d3dpp) );
    XVIDEO_MODE VideoMode;
    XGetVideoMode( &VideoMode );
    g_bWidescreen = VideoMode.fIsWideScreen;
    d3dpp.BackBufferWidth        = /*min( VideoMode.dwDisplayWidth,*/ 1280 /*)*/;
    d3dpp.BackBufferHeight       = /*min( VideoMode.dwDisplayHeight,*/ 720 /*)*/;
    d3dpp.BackBufferFormat       = D3DFMT_X8R8G8B8;
    d3dpp.BackBufferCount        = 1;
	d3dpp.MultiSampleType		 = D3DMULTISAMPLE_NONE;
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
    d3dpp.SwapEffect             = D3DSWAPEFFECT_DISCARD;
    d3dpp.PresentationInterval   = D3DPRESENT_INTERVAL_ONE;

    // Create the Direct3D device.
    if( FAILED( pD3D->CreateDevice( 0, D3DDEVTYPE_HAL, NULL,
                                    D3DCREATE_HARDWARE_VERTEXPROCESSING,
                                    &d3dpp, &g_pd3dDevice ) ) )
        return E_FAIL;

    return S_OK;
}

void InitThread() {
	// Thread that sets up storage, etc

	// Storage
	StorageInit();

	// Settings
	SettingsInit();

	// CDVDRaw
	CDVDopen("");
}

//-------------------------------------------------------------------------------------
// Name: main()
// Desc: The application's entry point
//-------------------------------------------------------------------------------------
void __cdecl main()
{
	printf("I'm in your output box, trolling your debuggers!\r\n");

	XOnlineStartup();

	InitD3D();
	
	CMyApp app;

	HRESULT hr = app.InitShared(g_pd3dDevice, &d3dpp, XuiD3DXTextureLoader);
	//HRESULT hr = app.Init(XuiD3DXTextureLoader);
	if(FAILED(hr))
	{
		OutputDebugString("[XePS2]Failed Init\n");
		return;
	}

	hr = app.RegisterDefaultTypeface(L"Arial Unicode MS", L"file://game:/media/xarialuni.ttf");
	if(FAILED(hr))
	{
		OutputDebugString("[XePS2]Failed to register typeface\n");
		return;
	}

	hr = app.LoadSkin(L"file://game:/media/simple_scene_skin.xur");

	hr = app.LoadFirstScene(L"file://game:/media/", L"mainmenu.xur", NULL);
 
	// Now that xui is loaded, lets load settings
	CreateThread(0, 0, (LPTHREAD_START_ROUTINE)InitThread, 0, 0, 0);

	for(;;) {
		// RENDER GAME
		g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_STENCIL | D3DCLEAR_ZBUFFER,
			D3DCOLOR_ARGB( 255, 0, 0, 0), 1.0, 0);

		app.RunFrame();
		app.Render();
		XuiTimersRun();

		g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
	}
	//app.Run();

	app.Uninit();

	XOnlineCleanup();
}

